package cn.wuwenyao.db.doc.generator.service.impl;

import java.io.File;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import cn.wuwenyao.db.doc.generator.entity.TableInfo;
import cn.wuwenyao.db.doc.generator.utils.FreemarkerUtils;
import freemarker.template.Template;

/***
 * 生成文档服务-html实现
 * 
 * @author wwy
 *
 */
public final class HtmlGeneratorServiceImpl extends AbstractGeneratorServiceImpl
{
    
    /** 模板名称 */
    private String templateFileName = "htmlTemplate.html";
    
    @Override
    public void generateDbDoc()
        throws Exception
    {
        String databaseName = dbInfoDao.databaseName();
        List<TableInfo> tableInfos = dbInfoDao.tableInfoList();
        templateFileName = StringUtils.defaultIfBlank(generatorConfig.getTemplateFilePath(), templateFileName);
        // 获取模板
        Template template = FreemarkerUtils.getTemplate(templateFileName);
        File file = this.createFile("html");
        Map<String, Object> map = new HashMap<>(2);
        map.put("tableInfos", tableInfos);
        map.put("databaseName", databaseName);
        // 根据模板生成文件
        template.process(map, new FileWriter(file));
    }
    
}
